package com.edu.d1_set;

import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    /**
     * 观察TreeSet对于值特性的数据如何排序
     * 学会对自定义类型的对象进行指定排序
     */
    public static void main(String[] args) {

        Set<Integer> treeSet = new TreeSet<>();//不重复、无索引、可排序
        treeSet.add(12);
        treeSet.add(77);
        treeSet.add(77);
        treeSet.add(3);
        treeSet.add(3);
        treeSet.add(5);
        System.out.println(treeSet);

        Set<String> treeSet1 = new TreeSet<>();//不重复、无索引、可排序
        treeSet1.add("Java");
        treeSet1.add("Java");
        treeSet1.add("About");
        treeSet1.add("Boy");
        treeSet1.add("Boy");
        treeSet1.add("girl");
        System.out.println(treeSet1);

        System.out.println("-------------------------");
        /**
         * 方式二：集合自定义比较规则
         * 当两种规则同时存在时，就近原则，优先使用集合自定义规则
         */
        /*TreeSet<Apple> treeSet2 = new TreeSet<>((apple,  t1)->   Double.compare(apple.getPrice(),t1.getPrice()));

              //  return t1.getWeight()-apple.getWeight()>=0?1:-1;//降序

                //注意：浮点型建议直接使用Double.compare()进行比较*/
        //完整格式
/*        new TreeSet<>(new Comparator<Apple>() {
            @Override
            public int compare(Apple apple, Apple t1) {
                return apple.getWeight()-t1.getWeight()>=0?1:-1;            }
        });*/
        TreeSet<Apple> treeSet2 = new TreeSet<>();
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("富士山","红苹果",99.8,100));
        treeSet2.add(new Apple("士山","黄苹果",22.5,300));
        treeSet2.add(new Apple("士山","黄苹果",22.5,300));
        treeSet2.add(new Apple("富","黑苹果",77.4,300));
        System.out.println(treeSet2);
    }
}
